package com.lyh.poi.controller.easyPoi;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.alibaba.fastjson.JSON;
import com.lyh.poi.dto.ExcelImportDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

/**
 * @author lyh
 * @date 2023-07-01
 */
@Api(value = "excel基于图片导入导出",tags = "excel基于图片导入导出")
@RestController
@RequestMapping("/easyPoi")
public class ExcelImgController {

    @PostMapping(value = "/import")
    @ApiOperation(value = "导入")
    public List<ExcelImportDTO> importData(MultipartFile file) {
        List<ExcelImportDTO> result = null;
        try {
            ImportParams params = new ImportParams();
            params.setNeedSave(true);
            result = ExcelImportUtil.importExcel(file.getInputStream(), ExcelImportDTO.class, params);
            System.out.println("导入数据："+ JSON.toJSONString(result));
            File file1 = new File(result.get(0).getCompanyLogo());
            System.out.println(file1.getAbsolutePath());
            System.out.println(file1.getPath());
            System.out.println(file1.getCanonicalPath());
            //file1.delete();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    @GetMapping(value = "/export")
    @ApiOperation(value = "导出")
    public void importData(HttpServletResponse response) {
        List<ExcelImportDTO> list = new ArrayList<>();
        ExcelImportDTO excelImportDTO = new ExcelImportDTO();
        excelImportDTO.setUserName("李华");
        excelImportDTO.setUserPhone("18886330642");
        // 保存图片的路径
        excelImportDTO.setCompanyLogo("E:\\excel\\upload\\img\\ExcelImportDTO\\pic80857091825.PNG");
        // https://lmg.jj20.com/up/allimg/4k/s/02/2109250006343S5-0-lp.jpg
        list.add(excelImportDTO);
        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(), ExcelImportDTO.class, list);
        ServletOutputStream out = null;
        try {
            response.setHeader("content-type", "application/octet-stream");
            response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode("测试22" + ".xls", "UTF-8"));
            out = response.getOutputStream();
            workbook.write(out);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (null != out) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}
